//
//  MeatPayViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/29.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatPayViewController.h"
#import "MeatPaySuccessViewController.h"


@interface MeatPayViewController ()

/**
 tap
 */
@property (nonatomic, strong) UITapGestureRecognizer *tap;


/**
 footer
 */
@property (nonatomic, strong) UIView *footer;


/**
 icons
 */
@property (nonatomic, strong) NSArray *icons;


/**
 titles
 */
@property (nonatomic, strong) NSArray *titles;


/**
 textfield
 */
@property (nonatomic, strong) UITextField *textField; //



@end

@implementation MeatPayViewController

#pragma mark - lifecycle methods

- (void)setupUI{
    
    self.baseNavView.navTitleLabel.text = @"确认订单";
    
//    self.currentIndex = 0;
//    [self addkeyBoardNotification];
//    
//    [self.view addSubview:self.tipLabel];
//    [self.view addSubview:self.footer];
//    
//    _tipLabel.mj_y = KScreenHeight - (_tipLabel.height + _footer.height +TJ_NavBarHeight);
//    _footer.mj_y = _tipLabel.bottom;
//    
//    self.tableView.mj_y = 0;
//    self.tableView.height = _tipLabel.top  - 5;
}

#pragma mark - networking methods
#pragma mark - response methods

- (void)submitPay{
    
    @weakify(self);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认放弃付款吗？" message:@"超过支付时效后，订单会被取消哦" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定离开" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [cancelAction setValue:UIColorFromRGB(0x363636) forKey:@"titleTextColor"];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"继续支付" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        @strongify(self);
        MeatPaySuccessViewController *vc = [[MeatPaySuccessViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
    [okAction setValue:mMainColor forKey:@"titleTextColor"];
    
    
    [alert addAction:cancelAction];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];

}

#pragma mark - public methods



#pragma mark - tableview delegate/dataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSInteger row = 1;
    if (section == 1) {
        row = 3;
    }else if (section == 2){
        return row = 4;
    }
    return row;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row == 0 ?42 : 54;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = mBackgroudColor;
    return sectionView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath .section == 0) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"UITableViewCell"];
        }
        
        cell.textLabel.text = @"应付金额：";
        cell.textLabel.textColor = UIColorFromRGB(0x262626);
        cell.textLabel.font = mFont(15);
        
        cell.detailTextLabel.text = @"¥357";
        cell.detailTextLabel.textColor = UIColorFromRGB(0xF25C62);
        cell.detailTextLabel.font = mFont(15);

        return cell;
    }
    
    
    UITableViewCell *newsCell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!newsCell) {
        newsCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
    }
    newsCell.textLabel.font = mFont(15);
    
    if (indexPath.row != 0) {
        
        newsCell.accessoryView = ({
            UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"meat_pay_unchoosed"]];
            CGRect frame = imgV.frame;
            frame.size = CGSizeMake(18, 18);
            imgV.frame = frame;
            [imgV setContentMode:UIViewContentModeLeft];
            imgV;
        });
    }
    
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            newsCell.textLabel.text = @"第三方支付方式";
            newsCell.textLabel.textColor = UIColorFromRGB(0x262626);
            
            if ([newsCell respondsToSelector:@selector(setSeparatorInset:)]) {
                [newsCell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            }

        }else{
            
            newsCell.textLabel.textColor = UIColorFromRGB(0x333333);
            newsCell.textLabel.text = self.titles[0][indexPath.row - 1];
            newsCell.imageView.image = [UIImage imageNamed:self.icons[0][indexPath.row - 1]];
        }
    }
    
    if (indexPath.section == 2) {
        
        if (indexPath.row == 0) {
            newsCell.textLabel.text = @"账户支付";
            newsCell.textLabel.textColor = UIColorFromRGB(0x262626);

            if ([newsCell respondsToSelector:@selector(setSeparatorInset:)]) {
                [newsCell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
            }

        }else{
           
            newsCell.textLabel.textColor = indexPath.row == 3 ?UIColorFromRGB(0x999999) :UIColorFromRGB(0x333333);
            newsCell.textLabel.font = indexPath.row == 3 ?mFont(12) :mFont(15);

            newsCell.textLabel.text = self.titles[1][indexPath.row - 1];
            newsCell.imageView.image = [UIImage imageNamed:self.icons[1][indexPath.row - 1]];
            newsCell.imageView.hidden = indexPath.row == 1? NO :YES;
            newsCell.accessoryView.hidden = indexPath.row != 3 ?NO :YES;
            
            
            if (indexPath.row == 2) {
                
                [newsCell addSubview:self.textField];
                newsCell.accessoryView = ({
                    UIButton *button = [UIButton makeButtonWithText:@"去充值" textColor:mMainColor font:mFont(12) textAliment:1 cornerRadius:5];
                    button.layer.borderWidth = 1;
                    button.layer.borderColor = mMainColor.CGColor;
                    CGRect frame = button.frame;
                    frame.size = CGSizeMake(56, 27);
                    button.frame = frame;
                    [button setContentMode:UIViewContentModeLeft];
                    
                    kWeakSelf(self);
                    button.tapBlock = ^{
                        
                        NSLog(@"去充值");
                    };
                    button;
                });
            }
            
        }
    }

    return newsCell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView  cellForRowAtIndexPath:indexPath];
    if (cell.accessoryView && !cell.accessoryView.hidden) {
        cell.accessoryView = ({
            UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"meat_pay_choosed"]];
            CGRect frame = imgV.frame;
            frame.size = CGSizeMake(18, 18);
            imgV.frame = frame;
            [imgV setContentMode:UIViewContentModeLeft];
            imgV;
        });
    }
}


#pragma mark - setter and getter

#pragma mark - override methods

- (void)registerTableViewCell{
    
    //[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    self.tableView.height = KScreenHeight - (TJ_NavBarHeight);
    self.tableView.mj_y = self.baseNavView.bottom;
    self.tableView.tableFooterView = self.footer;
    self.tableView.separatorColor = mIconLineColor;
}


- (UIView *)footer{
    if (!_footer) {
        _footer = [[UIView alloc] init];
        _footer.backgroundColor = mBackgroudColor;
        _footer.frame = CGRectMake(0, 0, KScreenWidth, 148);
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"去支付" forState:UIControlStateNormal];
        [button setTitleColor:KWhiteColor forState:UIControlStateNormal];
        button.titleLabel.font = mMediumFont(16);
        UIImageView *bg = [[UIImageView alloc] init];
        bg.image = [UIImage imageNamed:@"meat_buy_bg"];
        bg.frame = CGRectMake(0, 0, KScreenWidth - 60, 48);
        [self addShadowAndCircleCorner:bg.layer corner:5];

        [button insertSubview:bg atIndex:0];
        button.frame = CGRectMake(30, 50, KScreenWidth -60, 48);
        
        [button addTarget:self action:@selector(submitPay) forControlEvents:UIControlEventTouchUpInside];
        [_footer addSubview:button];
    }
    return _footer;
}


- (UITextField *)textField {
    
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.placeholder = @"本次充值最多可以充值多少元";
        _textField.font = mFont(12);
        _textField.size = CGSizeMake(200, 54);
        _textField.mj_x = 70;
        _textField.mj_y = 0;
        _textField.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _textField;
}


- (NSArray *)icons{
    
    if (!_icons) {
        _icons = @[@[@"meat_pay_alipay", @"meat_pay_wechat"],  @[@"meat_pay_yue", @"meat_pay_yue", @"meat_pay_yue"]];
    }
    return _icons;
}


- (NSArray *)titles{
    
    if (!_titles) {
        _titles = @[@[@"微信支付", @"支付宝"],  @[@"余额支付", @"¥", @"可用余额42.35元"]];
    }
    return _titles;
}


#pragma mark - other methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
    CGFloat sectionHeaderHeight = 10;
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


#pragma mark - notification methods

- (void)textFieldTextChanged:(NSNotification *)notification
{
    UITextField *textField = notification.object;
    
   // NSLog( @"______ 第 %ld 组  %@",  self.currentIndex, textField.text);
    // self.model.phone = textField.text;
}

- (void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    kWeakSelf(self);
    [UIView animateWithDuration:duration animations:^{
        weakself.tableView.mj_y = 0;
    }];
    
    //当键盘升起的时候添加点击降落操作
    if (!self.tap) {
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    }
    [self.view addGestureRecognizer:self.tap];
}

//取消键盘响应
- (void)hideKeyboard{
    
    [self.view endEditing:YES];
}


- (void)keyboardWillHide:(NSNotification *)notification{
    
    self.tableView.mj_y = 0;
    if (self.tap) {
        [self.view removeGestureRecognizer:self.tap];
    }
}


//添加键盘通知
- (void)addkeyBoardNotification{
    
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [kNotificationCenter addObserver:self selector:@selector(textFieldTextChanged:) name:UITextFieldTextDidChangeNotification object:nil];
}

//移除键盘通知
- (void)removeKeyboardNotification{
    
    [kNotificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [kNotificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [kNotificationCenter removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)dealloc{
    
    [self removeKeyboardNotification];
}




@end
